package com.finance.controller;

import com.finance.domain.Income;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IncomeController extends BaseController {

    /**
     * 跳转user-mgr.html页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/adminIncomeIn")
    public String adminIncomeIn(Model model) {
        return "incomeIn-mgr";
    }

    /**
     * 跳转user-mgr.html页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/adminIncomeOut")
    public String adminMoneOut(Model model) {
        return "incomeOut-mgr";
    }

    /**
     * 通过ajax注册
     *
     * @param income
     * @return
     */
    @RequestMapping("/incomeAdd")
    @ResponseBody
    public Map<String, Object> incomeAdd(Income income) {
        Map<String, Object> map = new HashMap<>();
        income.setCreateBy(String.valueOf(session.getAttribute("USER_ID")));
        int value = incomeService.insertIncome(income);
        if (value == 0) {
            map.put("data", "error");
        } else {
            map.put("data", "success");
        }
        return map;
    }

    @RequestMapping("/deleteIncomeIn/{id}")
    public String deleteIncomeIn(@PathVariable int id) {
        int a = incomeService.deleteIncomeById(id);
        return "redirect:/adminIncomeIn";
    }

    @RequestMapping("/deleteIncomeOut/{id}")
    public String deleteIncomeOut(@PathVariable int id) {
        int a = incomeService.deleteIncomeById(id);
        return "redirect:/adminIncomeOut";
    }

    /**
     * 获取存取款列表
     *
     * @return
     */
    @RequestMapping("/adminGetIncomeInList")
    @ResponseBody
    public Map<String, Object> adminGetIncomeInList() {
        Map<String, Object> map = new HashMap<>();
        Income income = new Income();
        income.setType("收入");
        income.setCreateBy(String.valueOf(session.getAttribute("USER_ID")));
        List<Income> users = incomeService.selectIncomeList(income);
        map.put("data", users);
        return map;
    }

    /**
     * 获取借还款列表
     *
     * @return
     */
    @RequestMapping("/adminGetIncomeOutList")
    @ResponseBody
    public Map<String, Object> adminGetIncomeOutList() {
        Map<String, Object> map = new HashMap<>();
        Income income = new Income();
        income.setType("支出");
        income.setCreateBy(String.valueOf(session.getAttribute("USER_ID")));
        List<Income> users = incomeService.selectIncomeList(income);
        map.put("data", users);
        return map;
    }


    @ResponseBody
    @RequestMapping("/getTodayYesterdayAccess")
    public Map<String, Object> getTodayYesterdayAccess() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("info", "success");
        map.put("dataIn", incomeService.selectCurrentIn(String.valueOf(session.getAttribute("USER_ID"))));
        map.put("dataOut", incomeService.selectCurrentOut(String.valueOf(session.getAttribute("USER_ID"))));
        return map;
    }
}
